package org.syncsoft.overalt.gui.impl.window.shareFolder;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;

import org.syncsoft.overalt.common.api.Resource;
import org.syncsoft.overalt.domain.community.entity.Community;
import org.syncsoft.overalt.domain.folder.entity.TypeShare;
import org.syncsoft.overalt.domain.user.entity.User;
import org.syncsoft.overalt.gui.api.JValuedCheckBox;
import org.syncsoft.overalt.gui.api.window.AbstractWindow;
import org.syncsoft.overalt.gui.impl.componentPlugin.CheckBoxComponentPlugin;
import org.syncsoft.overalt.gui.impl.componentPlugin.TableUserComponentPlugin;
import org.syncsoft.overalt.gui.util.BuildFormWindow;

/**
 *  
 * @author tptfc
 *
 */
public class ShareFolderWindow extends AbstractWindow {
	private Map<TypeShare, JComponent> panels;
	
	private Collection<User> contacts;
	private Collection<Community> communities;
	
	{
		setPanels(new HashMap<TypeShare, JComponent>());
		setContacts(new HashSet<User>());
		setCommunities(new HashSet<Community>());
	}

	public ShareFolderWindow(ShareFolderWindowControll controller) {
		super(controller);
	}

	public ShareFolderWindow(int width, int height, ShareFolderWindowControll controller) {
		super(width, height, controller);
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindow#buildWindow()
	 */
	@Override
	protected void buildWindow() {
		setTitle(Resource.getProperty("overalt.label.addFile"));
		BuildFormWindow build = new BuildFormWindow(getPanel());
		
		JLabel label;

		label = new JLabel(Resource.getProperty("overalt.text.share.type"));
		
		Collection<TypeShare> items = new ArrayList<TypeShare>();
		items.add(TypeShare.MY_FILES);
		items.add(TypeShare.CONTACTS_FILE);
		items.add(TypeShare.COMMUNITY_FILES);
		
		JComboBox comboBox = newComboBox("typeShare", items);
		comboBox.addActionListener(new ChangePanel(comboBox));
		build.add(label, comboBox);
		
		JComponent myFiles = createMyFilesPanel();
		getPanels().put(TypeShare.MY_FILES, myFiles);
		build.add(myFiles);
		
		JComponent contactsFiles = createContactsFilesPanel();
		getPanels().put(TypeShare.CONTACTS_FILE, contactsFiles);
		build.add(contactsFiles);
		
		JComponent communitiesFiles = createCommunityFilesPanel();
		getPanels().put(TypeShare.COMMUNITY_FILES, communitiesFiles);
		build.add(communitiesFiles);
		
		
		label = new JLabel(Resource.getProperty("overalt.label.tags"));
		build.add(label, newTextField("tags"));
		
		addSaveAndCancelButtons(build);
	}
	
	/**
	 * 
	 * @return
	 */
	private JComponent createMyFilesPanel() {
		BuildFormWindow build = new BuildFormWindow();
		
		JLabel label = new JLabel(Resource.getProperty("overalt.label.action") + ":");
		JCheckBox checkBox = newCheckbox("autoTitleAndDescription", Resource.getProperty("overalt.text.automatic.text.and.description"));
		
		build.add(label, checkBox);
		
		JComponent component = build.build();
		component.setVisible(true);
		return component;
	}

	/**
	 * 
	 * @return
	 */
	private JComponent createContactsFilesPanel() {
		BuildFormWindow build = new BuildFormWindow();
		
		JLabel label = new JLabel(Resource.getProperty("overalt.label.contacts"));
		JTable contactsTable = newTableUser("contacts", getContacts());
		
		build.add(label, contactsTable);
		
		JComponent component = build.build();
		component.setVisible(false);
		return component;
	}
	
	/**
	 * 
	 * @return
	 */
	private JComponent createCommunityFilesPanel() {
		BuildFormWindow build = new BuildFormWindow();
		
		
		
		JLabel label = new JLabel(Resource.getProperty("overalt.label.communities"));
		JComboBox comboBox = newComboBox("community", getCommunities());
		
		build.add(label, comboBox);
		
		JComponent component = build.build();
		component.setVisible(false);
		return component;
	}
	
	/**
	 * 
	 * @author tptfc
	 *
	 */
	private class ChangePanel implements ActionListener {
		private JComboBox comboBox;
		
		public ChangePanel(JComboBox comboBox) {
			setComboBox(comboBox);
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			JComponent component = getPanels().get(getComboBox().getSelectedItem());
			
			for (JComponent components : getPanels().values()) {
				components.setVisible(false);
			}
			
			if (component != null) {
				component.setVisible(true);
			}
		}

		protected JComboBox getComboBox() {
			return comboBox;
		}

		protected void setComboBox(JComboBox comboBox) {
			this.comboBox = comboBox;
		}
	}
	
	
	/**
	 * 
	 * @param key
	 * @param name
	 * @return
	 */
	private JValuedCheckBox newCheckbox(String key, String name) {
		return getComponentPlugin(CheckBoxComponentPlugin.class).newCheckBox(key, name);
	}
	
	/**
	 * 
	 * @param key
	 * @param users
	 * @return
	 */
	private JTable newTableUser(String key, Collection<User> users) {
		return getComponentPlugin(TableUserComponentPlugin.class).newTable(key, users);
	}

	private Map<TypeShare, JComponent> getPanels() {
		return panels;
	}
	private void setPanels(Map<TypeShare, JComponent> panels) {
		this.panels = panels;
	}

	Collection<User> getContacts() {
		return contacts;
	}
	void setContacts(Collection<User> contacts) {
		this.contacts = contacts;
	}
	Collection<Community> getCommunities() {
		return communities;
	}
	void setCommunities(Collection<Community> communities) {
		this.communities = communities;
	}
}
